$(document).ready(function(){
	//$(".mobility-setup")
	$("body").on("click",".mobility-setup",function(){
		if($(".mobility-overlay").length == 0){
			// if($("#FormId").val()!=0){
				
				createdMobileDialog = $(createMobileDialog());
				$("body").append(createdMobileDialog);
				

				$(".mobility-overlay").click(function(e){
					if (e.target === this || e.target === $(this).children(".mobility-content-middle")[0] ){
				        //outer
				        $(this).fadeOut(function(){
				        	$(this).remove();
				        });
				    }else{
				    	// inner
				    }
				})

				$(createdMobileDialog).find(".mobility-close-container").on({
					"click":function(e){
						$(this).parents(".mobility-overlay").eq(0).trigger("click");
					},
					"mouseenter":function(e){
						$(this).children("span.fa.fa-times").css({
							"color":"black",
							"text-shadow":"0px 0px 10px white",
							"transition":"ease 0.5s"
						});
						$(this).css({
							"box-shadow":"0px 0px 10px white inset, 0px 0px 10px white",
							"background-color":"rgb(100,100,100)"
						})
						var cloned = $(this).clone();
						$(this).parent().prepend(cloned);
						cloned.css({
							"background-color":"rgba(100,100,100,0)",
							"border":"2px solid black",
							"opacity":"0.8"
						}).animate({
							top: -25,
							left: 352,
							width:34,
							height:34
						},function(){
							$(this).fadeOut(function(){
								$(this).remove();
							})
						});
						// var cloned_times = $(this).children("span.fa.fa-times").clone();
						// $(this).prepend(cloned_times)
						// cloned_times.css({
						// 	"position":"absolute"
						// })
						/*.children("span.fa.fa-times").css({
							"color":"black"
						})*/

					},
					"mouseleave":function(){
						$(this).children("span.fa.fa-times").css({
							"color":"white",
							"text-shadow":""
						});
						$(this).css({
							"box-shadow":"",
							"background-color":"black"
						})
					}

				})

				var mobilityLocationContent = $(createdMobileDialog).find(".mobility-body").eq(0).children("#sortable-mobile");

				mobilityLocationContent.sortable({
					containment: "parent",
					items: 'li',
					axis: 'y',
					// cursorAt: { top:0, left: 0 },
					tolerance: "pointer",
					start: function(e, ui){
						$(this).sortable( "refreshPositions" );
						$(this).sortable("option","dragHeight",$(ui.item).outerHeight());
						$(ui.item).css({
							"opacity":"0.8",
							"height":"30px",
						});
					},
					stop: function(e, ui){
						$(ui.item).css({
							"opacity":"",
							"height":($(this).sortable("option","dragHeight"))+"px"
						});
					},
					over: function(e, ui){
						console.log("MOBLITY SORTABLE",ui,this);
						$(this).sortable( "refreshPositions" );
					},
					// helper: function(e, ui) {
				 //        var helper = $(ui.item).clone(); // Untested - I create my helper using other means...
				 //        // jquery.ui.sortable will override width of class unless we set the style explicitly.
				 //        helper.css({'height': '30px'});
				 //        return helper;
				 //    },
					// revert:true,
					placeholder:"sortable-placeholder"
				});

				mobilityLocationContent.disableSelection();
				var mobility_workspace = $("body").data();
				
				mobility_workspace.manage_field_component_on_form = [];

				mobility_workspace.manage_field_component_on_form = collectFormComponentFields();

				mobility_workspace.manage_saved_ui_mobile = [];
				
				var mobility_html_content_str = "";
				try{
					mobility_html_content_str = $($(".form-workspace-mobility-json").text());
				}catch(error){
					console.log("NOT A VALID SAVED HTML CONTENT OF MOBILITY");
					mobility_html_content_str="";
				}

				mobilityLocationContent.html(mobility_html_content_str);
				
				mobility_workspace.manage_saved_ui_mobile = collectSavedMobilityComponentFields();
				console.log("ETO NA UNG COMPARE COLLECTED AND SAVED", mobility_workspace.manage_field_component_on_form,mobility_workspace.manage_saved_ui_mobile);
				console.log(mobility_workspace.manage_field_component_on_form)

				//checking existence
				for(index = 0 ; index < mobility_workspace.manage_saved_ui_mobile.length ; index++){
					for(index2 = 0 ; index2 < mobility_workspace.manage_field_component_on_form.length ; index2++){
						if(mobility_workspace.manage_saved_ui_mobile[index].elementFieldComponentMobileID ==  mobility_workspace.manage_field_component_on_form[index2].elementFieldComponentOnFormID){
							mobility_workspace.manage_saved_ui_mobile[index].lookupFoundBasedOnForm = true;
							mobility_workspace.manage_field_component_on_form[index2].lookupFoundBasedOnMobile = true;
						}
						
					}
					for(index2 = 0 ; index2 < mobility_workspace.manage_field_component_on_form["dynamicFields"].length ; index2++){
						console.log("CGLOCTCH",mobility_workspace.manage_saved_ui_mobile[index]["elementFieldComponentMobileID"],
							mobility_workspace.manage_field_component_on_form["dynamicFields"][index2]['elementFieldComponentOnFormID'])
						


						if(mobility_workspace.manage_saved_ui_mobile[index]["elementFieldComponentMobileID"] ==
							mobility_workspace.manage_field_component_on_form["dynamicFields"][index2]["elementFieldComponentOnFormID"]
						){
							mobility_workspace.manage_saved_ui_mobile[index]["lookupFoundBasedOnForm"] = true;
							mobility_workspace.manage_field_component_on_form["dynamicFields"][index2]["lookupFoundBasedOnMobile"] = true;
						}
					}
				}

				//removing a non exisiting fields on the ui mobile based on the form
				for(index = 0 ; index < mobility_workspace.manage_saved_ui_mobile.length; index++){
					if(mobility_workspace.manage_saved_ui_mobile[index].lookupFoundBasedOnForm == false){
						mobility_workspace.manage_saved_ui_mobile[index].elementFieldComponentMobile.remove();
					}

				}

				//append fields not found on the saved mobile ui
				if(mobility_workspace.manage_field_component_on_form.length >= 1 ){
					for(ctr = 0 ; ctr < mobility_workspace.manage_field_component_on_form.length ; ctr++){
						if(mobility_workspace.manage_field_component_on_form[ctr].lookupFoundBasedOnMobile == false){
							eleComponent = mobility_workspace.manage_field_component_on_form[ctr].elementFieldComponentOnForm;
							fields_below = eleComponent.children(".fields_below");
							input_position_below = fields_below.children(".input_position_below");
							if(
								input_position_below.children(".tab-panel-parent-container").length >= 1 ||
								input_position_below.children(".table-wrapper").length >= 1
							){
								continue;
							}else{
								var li_ele_mobile = "";
								mobilityLocationContent.append(
									li_ele_mobile = $('<li class="element-mobile" style="position:relative;display:block;width:100%;">'+
										mobility_workspace.manage_field_component_on_form[ctr].elementFieldComponentOnForm.wrap("<span class='wrapper-fields' />").closest(".wrapper-fields").html()+
									'</li>')
								);
								li_ele_mobile.find(".component-primary-focus").removeClass("component-primary-focus");
								li_ele_mobile.find(".component-ancillary-focus").removeClass("component-ancillary-focus");
								li_ele_mobile.find(".setObject").css("width","97%");
								mobility_workspace.manage_field_component_on_form[ctr].elementFieldComponentOnForm.unwrap();
							}
						}
					}

					mobilityLocationContent.children("li").children(".setObject").css({
						"position":"relative",
						"top":"",
						"left":""
					});
					mobilityLocationContent.children("li").children(".setObject").children(".fields_below").css({
						"width":"100%"
					});
				}
				{//DYNAMIC FIELD KEY
					var dynamicTable = "", dynamicTableID = "", dynamicTableLabelID, dynamicTableAppended;
					var dynamicFields = "";
					var mobDynamicSetUp = "";
					var dis_app = "";
					for(var i in mobility_workspace.manage_field_component_on_form['dynamicFields']){
						// console.log("IFS",mobility_workspace.manage_field_component_on_form['dynamicFields'][i]);
						if(mobility_workspace.manage_field_component_on_form['dynamicFields'][i]['lookupFoundBasedOnMobile']==true){
							continue;
						}
						dynamicTable = mobility_workspace.manage_field_component_on_form['dynamicFields'][i]['elementFieldComponentOnForm'];
						dynamicTableID = dynamicTable.attr("data-object-id");
						dynamicTableLabelID = "#lbl_"+dynamicTableID;
						mobDynamicSetUp = '<li class="element-mobile mobDynamicTable" data-mob-dynamicTable-id="setObject_'+dynamicTableID+'" style="position:relative;display:inline-block;width:100%;">';
							mobDynamicSetUp += '<div class="label_below obj_label" id="label_'+ dynamicTableID +'"><label id="lbl_'+ dynamicTableID +'">'+ $(dynamicTableLabelID).text() +'</label></div>';
							mobDynamicSetUp += '<div class="tbdynamic-fields" style="width: calc(100% - 10px);outline: 1px solid gray;display: inline-block;margin: 5px;"></div>';
						mobDynamicSetUp += '</li>';
						dynamicTableAppended = $(mobDynamicSetUp);
						mobilityLocationContent.append(dynamicTableAppended);
						for(var j in mobility_workspace.manage_field_component_on_form['dynamicFields'][i]['fields']){
							dynamicFields = mobility_workspace.manage_field_component_on_form['dynamicFields'][i]['fields'][j]['elementFieldComponentOnForm'];
							
							if(dynamicFields.attr("data-type") == "labelOnly"){
								continue;
							}
							dynamicTableAppended.children(".tbdynamic-fields").eq(0).append(dis_app = $(dynamicFields.wrap("<span class='wrapper-fields' />").closest(".wrapper-fields").html() ) );
							odid = dis_app.attr("data-object-id");
							dynamicFields.unwrap();
							dis_app.removeClass("component-primary-focus");
							dis_app.removeClass("component-ancillary-focus");
							dis_app.css("width","97%");
							dis_app.css({
								"position":"relative",
								"top":"",
								"left":""
							});
							dis_app.children(".fields_below").css({
								"width":"100%"
							});
							dis_app.find(".label_below").css({
								"display":""
							});
							if(dynamicFields.attr("data-type") == "textArea"){
								dis_app.find("#obj_fields_"+odid).css({
									"height":"calc(100% - 20px)"
								});
							}
						}
						
					}
				}
				
			// }else{
			// 	createdMobileDialog = $(createMobileDialog());
			// 	$("body").append(createdMobileDialog);
			// 	$(".mobility-overlay").click(function(e){
			// 		if (e.target === this || e.target === $(this).children(".mobility-content-middle")[0] ){
			// 	        //outer
			// 	        $(this).fadeOut(function(){
			// 	        	$(this).remove();
			// 	        });
			// 	    }else{
			// 	    	// inner
			// 	    }
			// 	})
			// 	var mobilityLocationContent = $(createdMobileDialog).find(".mobility-body").eq(0).children("#sortable-mobile");

			// 	mobilityLocationContent.sortable({
			// 		containment: "parent",
			// 		items: 'li',
			// 		cursorAt: { top:1 },
			// 		// cursorAt: { top:0, left: 0 },
			// 		tolerance: "pointer",
			// 		start: function(e, ui){
			// 			$(this).sortable("option","dragHeight",$(ui.item).outerHeight());
			// 			$(ui.item).css({
			// 				"opacity":"0.8",
			// 				"height":"30px",
			// 			});
			// 		},
			// 		stop: function(e, ui){
			// 			$(ui.item).css({
			// 				"opacity":"",
			// 				"height":($(this).sortable("option","dragHeight"))+"px"
			// 			});
			// 		},
			// 		over: function(e, ui){
			// 			$(this).sortable( "refreshPositions" );
			// 		},
			// 		// helper: function() {
			// 	 //        var helper = $(this).clone(); // Untested - I create my helper using other means...
			// 	 //        // jquery.ui.sortable will override width of class unless we set the style explicitly.
			// 	 //        helper.css({'height': '30px'});
			// 	 //        return helper;
			// 	 //    },
			// 		revert:true,
			// 		placeholder:"sortable-placeholder"
			// 		// helper:function(){
			// 		// 	return $('<li class="element-mobile" style="height:50px;color:white;background-color:rgba(100,100,100,0.5);position:relative;display:inline-block;width:100%;">DRAGGING...</li>');
			// 		// }
			// 	});

			// 	mobilityLocationContent.disableSelection();
			// 	var mobility_workspace = $("body").data();
				
			// 	mobility_workspace.manage_field_component_on_form = [];

			// 	mobility_workspace.manage_field_component_on_form = collectFormComponentFields();

			// 	mobility_workspace.manage_saved_ui_mobile = [];
				
			// 	var mobility_html_content_str = $($(".form-workspace-mobility-json").text());
			// 	mobilityLocationContent.html(mobility_html_content_str);
				
			// 	mobility_workspace.manage_saved_ui_mobile = collectSavedMobilityComponentFields();
			// 	// console.log("ETO NA UNG COMPARE COLLECTED AND SAVED", mobility_workspace.manage_field_component_on_form,mobility_workspace.manage_saved_ui_mobile);
			// 	console.log(mobility_workspace.manage_field_component_on_form)

			// 	//checking existence
			// 	for(index = 0 ; index < mobility_workspace.manage_saved_ui_mobile.length ; index++){
			// 		for(index2 = 0 ; index2 < mobility_workspace.manage_field_component_on_form.length ; index2++){
			// 			if(mobility_workspace.manage_saved_ui_mobile[index].elementFieldComponentMobileID ==  mobility_workspace.manage_field_component_on_form[index2].elementFieldComponentOnFormID){
			// 				mobility_workspace.manage_saved_ui_mobile[index].lookupFoundBasedOnForm = true;
			// 				mobility_workspace.manage_field_component_on_form[index2].lookupFoundBasedOnMobile = true;
			// 			}
			// 		}
			// 	}

			// 	//removing a non exisiting fields on the ui mobile based on the form
			// 	for(index = 0 ; index < mobility_workspace.manage_saved_ui_mobile.length; index++){
			// 		if(mobility_workspace.manage_saved_ui_mobile[index].lookupFoundBasedOnForm == false){
			// 			mobility_workspace.manage_saved_ui_mobile[index].elementFieldComponentMobile.remove();
			// 		}
			// 	}

			// 	//append fields not found on the saved mobile ui
			// 	if(mobility_workspace.manage_field_component_on_form.length >= 1 ){
			// 		for(ctr = 0 ; ctr < mobility_workspace.manage_field_component_on_form.length ; ctr++){
			// 			if(mobility_workspace.manage_field_component_on_form[ctr].lookupFoundBasedOnMobile == false){
			// 				eleComponent = mobility_workspace.manage_field_component_on_form[ctr].elementFieldComponentOnForm;
			// 				fields_below = eleComponent.children(".fields_below");
			// 				input_position_below = fields_below.children(".input_position_below");
			// 				if(
			// 					input_position_below.children(".tab-panel-parent-container").length >= 1 ||
			// 					input_position_below.children(".table-wrapper").length >= 1
			// 				){
			// 					continue;
			// 				}else{
			// 					var li_ele_mobile = "";
			// 					mobilityLocationContent.append(
			// 						li_ele_mobile = $('<li class="element-mobile" style="position:relative;display:inline-block;width:100%;">'+
			// 							mobility_workspace.manage_field_component_on_form[ctr].elementFieldComponentOnForm.wrap("<span class='wrapper-fields' />").closest(".wrapper-fields").html()+
			// 						'</li>')
			// 					);
			// 					li_ele_mobile.find(".component-primary-focus").removeClass("component-primary-focus");
			// 					li_ele_mobile.find(".component-ancillary-focus").removeClass("component-ancillary-focus");
			// 					li_ele_mobile.find(".setObject").css("width","97%");
			// 					mobility_workspace.manage_field_component_on_form[ctr].elementFieldComponentOnForm.unwrap();
			// 				}
			// 			}
			// 		}

			// 		mobilityLocationContent.children("li").children(".setObject").css({
			// 			"position":"relative",
			// 			"top":"",
			// 			"left":""
			// 		});
			// 		mobilityLocationContent.children("li").children(".setObject").children(".fields_below").css({
			// 			"width":"100%"
			// 		});
			// 	}
			// }
			

		}else{

		}
	});

	$("body").on("click","#mobility-cancel.mobility-cancel",function(){
		$(this).closest(".mobility-overlay").fadeOut(300,function(){
			$(this).remove();
		});
	});
 
	$("body").on("click","#mobility-save.mobility-save",function(){
		var mobility_workspace = $("body").data();
		mobility_workspace.saving_request_format = {};
		mobility_workspace.saving_request_format.fields = [];
		var mobilityDynamicFields = [];
		var mobilityLocationContent = $("#mobility-save.mobility-save").closest(".mobility-content-center").children(".mobility-body").eq(0);
		// getting fieldss
		if(mobilityLocationContent.children("ul#sortable-mobile").children(".element-mobile").length >= 1){
			console.log("MOBILITY SAVING")
			mobilityLocationContent.children("ul#sortable-mobile").children(".element-mobile").each(function(){
				if($(this).hasClass("mobDynamicTable")){
					
					mobility_workspace.saving_request_format.fields.push({
						"id": $(this).attr("data-mob-dynamicTable-id"),
						"label": $(this).find(".obj_label").eq(0).text(),
						"type": "REPEATER"
					});
					var keyFieldDynamic = 0;
					$(this).find(".setObject").each(function(){
						object_index_id = $(this).attr("data-object-id");
						fields_below = $(this).children(".fields_below");
						mobilityDynamicFields.push({
							"id": $(this).find(".getFields_"+object_index_id).eq(0).attr("name").replace("[]",""),
		                    "label": fields_below.children("#label_"+object_index_id).children("#lbl_"+object_index_id).text(),
		                    "generation": "REQUESTOR",
						});
						form_field = fields_below.children("#obj_fields_"+object_index_id).find(".getFields_"+object_index_id+"");
						//ADDITIONAL FOR SPECIAL FIELDS
						if($(this).attr("data-type") == "computed"){
							mobilityDynamicFields[keyFieldDynamic]["generation"] = "SYSTEM";
						}
						// =========================================				                                                               
		                                
		                                if (form_field.attr("type") !== undefined) {
		                                    //alert(form_field.attr('name') + ' ' + form_field.attr("type") + ' ' + form_field.parents(".thisDynamicTable").eq(0).length);
		                                    
		                                    if (form_field.parents(".thisDynamicTable").eq(0).length > 0) {
		                                       // alert('from dynamic table');
		                                    }
		                                }
		                                
						if( form_field.length >= 1 && form_field.attr("type") ){ //BY INPUT TYPES
							if(form_field.attr("type") == "checkbox"){

								mobilityDynamicFields[keyFieldDynamic]["type"] = "CHECK_BOX_GROUP";
								mobilityDynamicFields[keyFieldDynamic]["options"] = [];
								form_field.each(function(eqi_ffc){
									mobilityDynamicFields[keyFieldDynamic]["options"].push($(this).val());
								});

							}else if(form_field.attr("type") == "radio"){

								mobilityDynamicFields[keyFieldDynamic]["type"] = "RADIO_BUTTON_GROUP";
								mobilityDynamicFields[keyFieldDynamic]["options"] = [];
								form_field.each(function(eqi_ffc){
									mobilityDynamicFields[keyFieldDynamic]["options"].push($(this).val());
								});
							}else if(form_field.attr("type") == "text"){
								if(form_field.attr("data-type") == "dateTime"){
									mobilityDynamicFields[keyFieldDynamic]["type"] = "DATE_TIME_PICKER";
								}else if(form_field.attr("data-type") == "date"){
									mobilityDynamicFields[keyFieldDynamic]["type"] = "DATE_PICKER";
								}else if(form_field.closest(".setObject").attr("data-type") == "computed"){
									mobilityDynamicFields[keyFieldDynamic]["type"] = "TEXT_FIELD";
									if(typeof form_field.attr("processor-type") != "undefined")
									{ mobilityDynamicFields[keyFieldDynamic]["processor-type"] = ""+form_field.attr("processor-type"); }
									if(typeof form_field.attr("processor") != "undefined")
									{ mobilityDynamicFields[keyFieldDynamic]["processor"] = ""+form_field.attr("processor"); }
								}else if(form_field.closest(".setObject").attr("data-type") == "barCodeScanner"){
									mobilityDynamicFields[keyFieldDynamic]["type"] = "BAR_CODE";
								} else{
									mobilityDynamicFields[keyFieldDynamic]["type"] = "TEXT_FIELD";

								}
							} else {
		                                            //alert(form_field.attr("type"));
		                                        }
						} else{ //BY TAG NAME
		                                    
		                                    //&& form_field.closest(".setObject").attr("data-type") == "dropdown"
		                                    
		                                    if (form_field.prop("tagName") == "SELECT") {
		                                        
		                                        if (form_field.attr("multiple")) {
		                                            mobilityDynamicFields[keyFieldDynamic]["type"] = "SELECT_MANY";
		                                            mobilityDynamicFields[keyFieldDynamic]["property"] = "MULTIPLE";                                                                                       
		                                            
		                                            //  include the options of this field in the JSON object
		                                            var options = [];
		                                            $(form_field).find('option').each(function() {
		                                                options.push($(this).attr('value'));
		                                            });
		                                            
		                                            mobilityDynamicFields[keyFieldDynamic]["options"] = options;
		                                            
		                                        } else if (form_field.closest(".setObject").attr("data-type") == "dropdown") {
		                                            mobilityDynamicFields[keyFieldDynamic]["type"] = "DROPDOWN";
		                                            mobilityDynamicFields[keyFieldDynamic]["property"] = "DROPDOWN";
		                                            
		                                            //  include the options of this field in the JSON object
		                                            var options = [];
		                                            $(form_field).find('option').each(function() {
		                                                options.push($(this).attr('value'));
		                                            });
		                                            
		                                            mobilityDynamicFields[keyFieldDynamic]["options"] = options;
		                                            
		                                        } else {
		                                            mobilityDynamicFields[keyFieldDynamic]["type"] = "SELECT";
		                                            mobilityDynamicFields[keyFieldDynamic]["property"] = "";
		                                        }
		                                        
		                                    } else if (form_field.prop("tagName") == "TEXTAREA") {
		                                        mobilityDynamicFields[keyFieldDynamic]["type"] = "TEXT_AREA";
		                                    }
						}
						keyFieldDynamic++;
					})
					push_length = mobility_workspace.saving_request_format.fields.length - 1;
					// console.log(mobilityDynamicFields)
					mobility_workspace.saving_request_format.fields[push_length]['repeater_fields'] = mobilityDynamicFields;
					mobilityDynamicFields = [];
				}else{
					object_index_id = $(this).children(".setObject").attr("data-object-id");
					fields_below = $(this).children(".setObject").children(".fields_below");
					mobility_workspace.saving_request_format.fields.push({
						"id": $(this).find(".getFields_"+object_index_id).eq(0).attr("name").replace("[]",""),
						"label": fields_below.children("#label_"+object_index_id).children("#lbl_"+object_index_id).text(),
						"generation": "REQUESTOR",
					});
					push_length = mobility_workspace.saving_request_format.fields.length - 1;
					form_field = fields_below.children("#obj_fields_"+object_index_id).find(".getFields_"+object_index_id+"");
					

					//ADDITIONAL FOR SPECIAL FIELDS
					if($(this).children(".setObject").attr("data-type") == "computed"){
						mobility_workspace.saving_request_format.fields[push_length]["generation"] = "SYSTEM";
					}
					// =========================================				                                                               
	                                
	                                if (form_field.attr("type") !== undefined) {
	                                    //alert(form_field.attr('name') + ' ' + form_field.attr("type") + ' ' + form_field.parents(".thisDynamicTable").eq(0).length);
	                                    
	                                    if (form_field.parents(".thisDynamicTable").eq(0).length > 0) {
	                                       // alert('from dynamic table');
	                                    }
	                                }
	                                
					if( form_field.length >= 1 && form_field.attr("type") ){ //BY INPUT TYPES
						if(form_field.attr("type") == "checkbox"){

							mobility_workspace.saving_request_format.fields[push_length]["type"] = "CHECK_BOX_GROUP";
							mobility_workspace.saving_request_format.fields[push_length]["options"] = [];
							form_field.each(function(eqi_ffc){
								mobility_workspace.saving_request_format.fields[push_length]["options"].push($(this).val());
							});

						}else if(form_field.attr("type") == "radio"){

							mobility_workspace.saving_request_format.fields[push_length]["type"] = "RADIO_BUTTON_GROUP";
							mobility_workspace.saving_request_format.fields[push_length]["options"] = [];
							form_field.each(function(eqi_ffc){
								mobility_workspace.saving_request_format.fields[push_length]["options"].push($(this).val());
							});
						}else if(form_field.attr("type") == "text"){
							if(form_field.attr("data-type") == "dateTime"){
								mobility_workspace.saving_request_format.fields[push_length]["type"] = "DATE_TIME_PICKER";
							}else if(form_field.attr("data-type") == "date"){
								mobility_workspace.saving_request_format.fields[push_length]["type"] = "DATE_PICKER";
							}else if(form_field.closest(".setObject").attr("data-type") == "computed"){
								mobility_workspace.saving_request_format.fields[push_length]["type"] = "TEXT_FIELD";
								if(typeof form_field.attr("processor-type") != "undefined")
								{ mobility_workspace.saving_request_format.fields[push_length]["processor-type"] = ""+form_field.attr("processor-type"); }
								if(typeof form_field.attr("processor") != "undefined")
								{ mobility_workspace.saving_request_format.fields[push_length]["processor"] = ""+form_field.attr("processor"); }
							}else if(form_field.closest(".setObject").attr("data-type") == "barCodeScanner"){
								mobility_workspace.saving_request_format.fields[push_length]["type"] = "BAR_CODE";
							} else{
								mobility_workspace.saving_request_format.fields[push_length]["type"] = "TEXT_FIELD";

							}
						} else {
	                                            //alert(form_field.attr("type"));
	                                        }
					} else{ //BY TAG NAME
	                                    
	                                    //&& form_field.closest(".setObject").attr("data-type") == "dropdown"
	                                    
	                                    if (form_field.prop("tagName") == "SELECT") {
	                                        
	                                        if (form_field.attr("multiple")) {
	                                            mobility_workspace.saving_request_format.fields[push_length]["type"] = "SELECT_MANY";
	                                            mobility_workspace.saving_request_format.fields[push_length]["property"] = "MULTIPLE";                                                                                       
	                                            
	                                            //  include the options of this field in the JSON object
	                                            var options = [];
	                                            $(form_field).find('option').each(function() {
	                                                options.push($(this).attr('value'));
	                                            });
	                                            
	                                            mobility_workspace.saving_request_format.fields[push_length]["options"] = options;
	                                            
	                                        } else if (form_field.closest(".setObject").attr("data-type") == "dropdown") {
	                                            mobility_workspace.saving_request_format.fields[push_length]["type"] = "DROPDOWN";
	                                            mobility_workspace.saving_request_format.fields[push_length]["property"] = "DROPDOWN";
	                                            
	                                            //  include the options of this field in the JSON object
	                                            var options = [];
	                                            $(form_field).find('option').each(function() {
	                                                options.push($(this).attr('value'));
	                                            });
	                                            
	                                            mobility_workspace.saving_request_format.fields[push_length]["options"] = options;
	                                            
	                                        } else {
	                                            mobility_workspace.saving_request_format.fields[push_length]["type"] = "SELECT";
	                                            mobility_workspace.saving_request_format.fields[push_length]["property"] = "";
	                                        }
	                                        
	                                    } else if (form_field.prop("tagName") == "TEXTAREA") {
	                                        mobility_workspace.saving_request_format.fields[push_length]["type"] = "TEXT_AREA";
	                                    }
					}
                }                                      
				// console.log(JSON.stringify(mobility_workspace.saving_request_format.fields[push_length]));
			});
		}
		
		if(typeof mobility_workspace.manage_field_component_on_form != "undefined"){
			delete mobility_workspace.manage_field_component_on_form;	
		}
		if(typeof mobility_workspace.manage_saved_ui_mobile != "undefined"){
			delete mobility_workspace.manage_saved_ui_mobile;
		}
		mobility_workspace.saved_mobile_content = $(this).closest(".mobility-content-center").children(".mobility-body").children("#sortable-mobile").html();
		$("body").data(mobility_workspace);
		$(".form-workspace-mobility-json").text(mobilityLocationContent.children("#sortable-mobile").html());
		showNotification({
		    message: "Mobility was successfully saved!",
		    type: "success",
		    autoClose: true,
		    duration: 3
	    });
		// $(".mobility-overlay").click();
		// console.log("TERSTYYING");
		// console.log(mobility_workspace);
		// console.log(mobility_workspace.saving_request_format);
		// console.log(JSON.stringify(mobility_workspace.saving_request_format));
		console.log($("body").data())
	});



	function createMobileDialog (){
		var ret
		=	'<div class="mobility-overlay" title="click the overlay background to close the dialog" style="background-color:rgba(225,225,225,0.5);z-index:1000000;top:0px;display:table;position:fixed;width:100%;height:100%;">'+
				'<div class="mobility-content-middle" style="height:auto;display:table-cell;vertical-align:middle;">'+
					
					'<div class="mobility-content-center" style="height:500px;width:300px;border:2px solid #FFFFFF;padding-left:35px;padding-right:35px;padding-top:25px;background-color:#141414;border-radius: 15px;position: relative;box-shadow: 0 0 5px 5px rgb(109, 109, 109);margin: 0px auto;">'+
						'<div class="mobility-close-container" style="cursor:pointer;font-size: 23px;position: absolute;top: -20px;left: 357px;color: white;background-color: black;border: 2px solid white;border-radius: 100%;width: 24px;height: 24px;text-align:center;"><span class="fa fa-times" style="color:white;"></span></div>'+
						'<div class="mobility-header" style="height:35px;"><div class="head-design" style="background-color: rgb(200,200,200);height: 10px;width: 100px;margin: 0px auto;border-radius: 10px;box-shadow: 3px 3px 5px black inset;"></div></div>'+
						'<div class="mobility-body" style="position:relative;height:400px;width:100%;border:1px solid #000;background-color:#FFF;border-radius: 10px;overflow:auto">'+
							'<ul id="sortable-mobile" style="position:relative;"></ul>'+
						'</div>'+
						'<div class="mobility-footer">'+
							'<div class="fields" style="border:0px;">'+
								//'<div class="label_basic"></div>'+
								//'<div class="input_position" style="margin-top:5px;text-align:center;">'+
									'<input type="button" class="btn-blueBtn mobility-save" data-form-type="formbuilder" id="mobility-save" value="Save" style="margin: 0px auto;float: none;clear: both;display: inherit;border-radius: 100%;width: 35px;height: 35px;margin-top: 10px;box-shadow: 0px 15px 15px black inset;text-align: center;padding: 0px;">'+
									//'<input type="button" class="btn-basicBtn mobility-cancel" id="mobility-cancel" value="Cancel">'+
								//'</div>'+
							'</div>'+
						'</div>'+
					'</div>'+
				'</div>'+
			'</div>';

		return ret;
	}

	function collectSavedMobilityComponentFields(){
		var collect_saved_ui_mobile = [];
		$("#sortable-mobile .element-mobile").each(function(eqs){
			collect_saved_ui_mobile.push({
				"elementFieldComponentMobile":$("#sortable-mobile .element-mobile").eq(eqs),
				"lookupFoundBasedOnForm":false,
				"elementFieldComponentMobileID":$("#sortable-mobile .element-mobile").eq(eqs).find(".setObject").attr("id"),
				"eqIndex":eqs
			});
			if($("#sortable-mobile .element-mobile").eq(eqs).hasClass("mobDynamicTable")){
				collect_saved_ui_mobile[collect_saved_ui_mobile.length -1]["elementFieldComponentMobileID"] = $("#sortable-mobile .element-mobile").eq(eqs).attr("data-mob-dynamictable-id");
			}
		});
		return collect_saved_ui_mobile;
	}

	function collectFormComponentFields(){
		var collect_form_component_fields = [];
		var collect_form_component_dynamic_table = [];
		var collect_form_component_dynamic_table_fields = [];
		$(".workspace.formbuilder_ws .setObject ").each(function(eqis){
			if($(".workspace.formbuilder_ws .setObject ").eq(eqis).find(".getFields").attr("name")){

			}else{
				return true;
			}
			if($(this).closest(".thisDynamicTable[repeater-table='true']").length>0){
				return;
			}
			//dynamic table will not push in array
			if($(this).find(".thisDynamicTable").eq(0).length>0){
				if($(this).find(".thisDynamicTable").eq(0).attr("repeater-table")){
					if($(this).find(".thisDynamicTable").eq(0).attr("repeater-table")=="true"){
						return;
					}
				}
			}
			collect_form_component_fields.push({
				"elementFieldComponentOnForm":$(".workspace.formbuilder_ws .setObject ").eq(eqis),
				"lookupFoundBasedOnMobile":false,
				"elementFieldComponentOnFormID":$(".workspace.formbuilder_ws .setObject ").eq(eqis).attr("id")
			});
		});
		var index = 0;
		$(".thisDynamicTable[repeater-table='true']").each(function(eqisTable){
			//dynamic table
			collect_form_component_dynamic_table.push({
				"elementFieldComponentOnForm":$(this).closest(".setObject"),
				"lookupFoundBasedOnMobile":false,
				"elementFieldComponentOnFormID":$(this).closest(".setObject").attr("id")
			});
			$(this).find(".setObject").each(function(eqisFields){
				//field data in dynamic table
				collect_form_component_dynamic_table_fields.push({
					"elementFieldComponentOnForm":$(this),
					"lookupFoundBasedOnMobile":false,
					"elementFieldComponentOnFormID":$(this).attr("id")
				})
				collect_form_component_dynamic_table[index]['fields'] = collect_form_component_dynamic_table_fields;
			})
			collect_form_component_dynamic_table_fields = []; //clean array
			index++;
		})
		collect_form_component_fields['dynamicFields'] = collect_form_component_dynamic_table;
		return collect_form_component_fields;
	}

	function removeFieldNotExistingOnForm(){
		//
	}

	//

	function acceptableField(){
		//
	}
});